package com.baipeng.bp_alicloud_push;

import android.util.Log;

import androidx.annotation.NonNull;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.baipeng.bp_alicloud_push.application.PushApplication;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

/**
 * BpAlicloudPushPlugin
 */
public class BpAlicloudPushPlugin implements FlutterPlugin, MethodCallHandler {
    private static final String TAG = "MPS:BPPlugin";

    /// The MethodChannel that will the communication between Flutter and native Android
    ///
    /// This local reference serves to register the plugin with the Flutter Engine and unregister it
    /// when the Flutter Engine is detached from the Activity
    private MethodChannel methodChannel;

    private EventChannel eventChannel;

    EventChannel.StreamHandler streamHandler = new EventChannel.StreamHandler() {

        @Override
        public void onListen(Object arguments, EventChannel.EventSink events) {
            PushApplication.eventSink = events;
        }

        @Override
        public void onCancel(Object arguments) {
            PushApplication.eventSink = null;
        }
    };

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
        methodChannel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "bp_alicloud_push");
        methodChannel.setMethodCallHandler(this);
        PushApplication.methodChannel = methodChannel;
        eventChannel = new EventChannel(flutterPluginBinding.getBinaryMessenger(), "bp_alicloud_push_event");
        eventChannel.setStreamHandler(streamHandler);
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
        switch (call.method) {
            case "getPlatformVersion":
                result.success("Android " + android.os.Build.VERSION.RELEASE);
                break;
            case "bindAccount":
                String account = call.argument("account");
                bindAccount(account);
                result.success(null);
                break;
            case "unbindAccount":
                unbindAccount();
                result.success(null);
                break;
            default:
                result.notImplemented();
                break;
        }
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
        methodChannel.setMethodCallHandler(null);
    }

    void bindAccount(String account) {
        Log.i(TAG, "bindAccount: " + account);
        CloudPushService cloudPushService = PushServiceFactory.getCloudPushService();
        cloudPushService.bindAccount(account, new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                Log.i(TAG, "bind account " + account + " success");
            }

            @Override
            public void onFailed(String errorCode, String errorMsg) {
                Log.e(TAG, "bind account " + account + " failed." + "errorCode: " + errorCode + ", errorMsg:" + errorMsg);
            }
        });
    }

    void unbindAccount() {
        CloudPushService cloudPushService = PushServiceFactory.getCloudPushService();
        cloudPushService.unbindAccount(new CommonCallback() {
            @Override
            public void onSuccess(String s) {
                Log.i(TAG, "unbind account success");
            }

            @Override
            public void onFailed(String errorCode, String errorMsg) {
                Log.e(TAG, "bind account failed." + "errorCode: " + errorCode + ", errorMsg:" + errorMsg + "\n");
            }
        });
    }
}
